﻿/*
[]
*/

/*
文件头的注释块\/*[]*\/中“[]”中是本js依赖的其他文件
*/

/*
文件系统，分为前端、后端。前端主要为文件描述符(_nodelist)和为展现服务的一些资源（如图标）；后端存储实际文件。
目前将前后端合并了。
文件系统的另一部分为文件mime信息，分为系统默认mime（在etc的mime文件中配置）和动态注册mime。优先使用默认mime。
*/

Ext.namespace("tk","tk.yunos","tk.yunos.filesystem");

tk.yunos.filesystem.FILESYSTEM_ETC_DIR = 'filesystem/etc';
tk.yunos.filesystem.FRONTEND_ROOT_PATH = 'filesystem/root';
tk.yunos.filesystem.BACKEND_ROOT_PATH = 'filesystem/root';
tk.yunos.filesystem.NODE_LIST_FILE = '.node.list';
tk.yunos.filesystem.APP_ICON_DIR = 'filesystem/etc/app_icons';
tk.yunos.filesystem.APP_DIR = 'apps';
tk.yunos.filesystem.LOGO = 'resources/logo.png';

tk.yunos.filesystem.DEFAULT_MIME = [];
tk.yunos.filesystem.DYNAMIC_MIME = [];
tk.yunos.filesystem.APP_INFO = [];

//加载文件系统配置
tk.yunos.filesystem.loadConfigs = function(onload){
	tk.yunos.filesystem.loadDefaultMime(function(){
		tk.yunos.filesystem.loadAppInfo(onload);
	});
};

//加载系统默认mime
tk.yunos.filesystem.loadDefaultMime = function(onload){
	Ext.Ajax.request({
		url: tk.yunos.filesystem.FILESYSTEM_ETC_DIR + '/mime',
		method: 'post',
		callback: function(options, success, response){
			if(success){
				var mimes = Ext.decode(response.responseText);
				for(var i = 0; i < mimes.length; i++){
					if(!YOSUtil.isBlankString(mimes[i].icon)){
						mimes[i].icon = tk.yunos.filesystem.FILESYSTEM_ETC_DIR + '/icons/' + mimes[i].icon;
					}
					tk.yunos.filesystem.DEFAULT_MIME[mimes[i].mime] = mimes[i];
				}
			}else{
				Ext.Msg.alert('警告', '加载mime出错，请刷新网页重新加载');
			}
			onload.call();
		}
	});
};

//加载应用安装信息
tk.yunos.filesystem.loadAppInfo = function(onload){
	Ext.Ajax.request({
		url: tk.yunos.filesystem.FILESYSTEM_ETC_DIR + '/apps',
		method: 'post',
		callback: function(options, success, response){
			if(success){
				var apps = Ext.decode(response.responseText);
				for(var i = 0; i < apps.length; i++){
					tk.yunos.filesystem.APP_INFO[apps[i].name] = apps[i];
				}
			}else{
				Ext.Msg.alert('警告', '加载应用安装信息出错，请刷新网页重新加载');
			}
			onload.call();
		}
	});
};

//注册mime
tk.yunos.filesystem.regMime = function(mime, openwith, icon){
	if(mime && openwith){
		if(tk.yunos.filesystem.DYNAMIC_MIME[mime]){
			//已存在该mime，暂不考虑，覆盖该mime
		}
		var newMime = {};
		newMime.mime = mime;
		newMime.openwith = openwith;
		if(icon){
			newMime.icon = icon;
		}
		tk.yunos.filesystem.DYNAMIC_MIME[mime] = newMime;
	}
};

//根据文件描述符执行打开文件
tk.yunos.filesystem.openFile = function(fileDescriptor, openParams){
	tk.yunos.system.wait();
	var app;
	if(!YOSUtil.isBlankString(fileDescriptor.mime)){
		//本地应用程序
		if(fileDescriptor.mime == 'local-app'){
			tk.yunos.filesystem.loadApp(fileDescriptor.app);
			app = eval(fileDescriptor.app);
		}else if(fileDescriptor.mime == 'link'){
			//如果是链接，则跳转到链接处理程序
			app = tk.yunos.system.Linker;
		}else{
			var myMime = tk.yunos.filesystem.DEFAULT_MIME[fileDescriptor.mime] 
				|| tk.yunos.filesystem.DYNAMIC_MIME[fileDescriptor.mime];
			if(myMime){
				if(!YOSUtil.isBlankString(myMime.openwith)){
					tk.yunos.filesystem.loadApp(myMime.openwith);
					app = eval(myMime.openwith);
				}
			}
		}
	}else{
		Ext.Msg.alert("警告", "未知类型文件");
	}
	if(app && typeof(app) == 'function'){
		app.call(app, fileDescriptor, openParams);
	}else{
		Ext.Msg.alert("警告", "文件类型为" + fileDescriptor.mime + "的应用程序注册错误");
	}
	tk.yunos.system.finish();
};

tk.yunos.filesystem.COPY_QUEUE = new YOSUtil.SingleArray();

//根据文件在本文件系统中的地址获取其实际URL
tk.yunos.filesystem.getBackendAddress = function(address){
	return encodeURI(tk.yunos.filesystem.BACKEND_ROOT_PATH + '/' + address);
};

//根据文件在本文件系统中的地址获取其文件描述符
tk.yunos.filesystem.getFileDescriptor = function(address, callback, sync){
	var lastSplitPos = address.lastIndexOf('/');
	var dir = address.substr(0, lastSplitPos);
	var fileName = address.substr(lastSplitPos + 1);
	fileName = fileName.replace(/\/+$/g, "");
	tk.yunos.filesystem.getFiles(dir, function(success, files){
		var fileDescriptor = null;
		if(success){
			for(var i = 0; i < files.length; i++){
				if(files[i].name == fileName){
					fileDescriptor = files[i];
					break;
				}
			}
		}
		callback.call(this, success, fileDescriptor);
	}, sync);
};

//根据文件地址获取文件内容
//参数address：文件绝对地址
//callback：回调函数，function(success, fileContent),success-是否成功，fileContent文件内容
//sync: 是否是同步请求
tk.yunos.filesystem.getFileContent = function(address, callback, sync){
	Ext.Ajax.request({
		url: tk.yunos.filesystem.getBackendAddress(address),
		method: 'post',
		sync: sync || false,
		callback: function(options, success, response){
			callback.call(this, success, response.responseText);
		}
	});
};

//根据文件地址获取文件夹中的所有文件
//参数dir：文件夹绝对地址
//callback：回调函数，function(success, files),success-是否成功，files文件夹中所有文件的文件描述符
//sync: 是否是同步请求
tk.yunos.filesystem.getFiles = function(dir, callback, sync){
	Ext.Ajax.request({
		url: encodeURI(tk.yunos.filesystem.FRONTEND_ROOT_PATH + dir + '/' + tk.yunos.filesystem.NODE_LIST_FILE),
		method: 'post',
		sync: sync || false,
		callback: function(options, success, response){
			files = [];
			if(success){
				files = Ext.decode(response.responseText);
				//处理图标
				for(var i = 0; i < files.length; i++){
					if(YOSUtil.isBlankString(files[i].icon)){
						if(tk.yunos.filesystem.DEFAULT_MIME[files[i].mime]){
							files[i].icon = tk.yunos.filesystem.DEFAULT_MIME[files[i].mime].icon;
						}else if(tk.yunos.filesystem.DYNAMIC_MIME[files[i].mime]){
							files[i].icon = tk.yunos.filesystem.DYNAMIC_MIME[files[i].mime].icon;
						}else{
							files[i].icon = tk.yunos.filesystem.LOGO;
						}
					}else{
						files[i].icon = tk.yunos.filesystem.APP_ICON_DIR + '/' + files[i].icon;
					}
					files[i].address = (dir + "/" + files[i].name).replace(/\/+/g, "\/");
				}
			}
			callback.call(this, success, files);
		}
	});
};

tk.yunos.filesystem.loadedApps = [
	//预加载的系统级应用
	'tk.yunos.system.FileMgr',
	'tk.yunos.system.Desktop',
	'tk.yunos.system.Browser',
	'tk.yunos.system.Linker'];
tk.yunos.filesystem.loadApp = function(appName){
	if(tk.yunos.filesystem.loadedApps.indexOf(appName) == -1){
		if(!tk.yunos.filesystem.APP_INFO[appName]){
			Ext.Msg.alert("警告", "未安装该应用：" + appName);
			return;
		}
		var appHome = tk.yunos.filesystem.APP_DIR + '/' + tk.yunos.filesystem.APP_INFO[appName].home;
		//加载css
		if(tk.yunos.filesystem.APP_INFO[appName].css){
			for(var i = 0; i < tk.yunos.filesystem.APP_INFO[appName].css.length; i++){
				YOSUtil.loadCss(appHome + '/css/' + tk.yunos.filesystem.APP_INFO[appName].css[i]);
			}
		}
		//加载js
		if(tk.yunos.filesystem.APP_INFO[appName].js){
			for(var i = 0; i < tk.yunos.filesystem.APP_INFO[appName].js.length; i++){
				YOSUtil.loadJs(appHome + '/js/' + tk.yunos.filesystem.APP_INFO[appName].js[i]);
			}
		}
		tk.yunos.filesystem.loadedApps.push(appName);
	}
};